<?php    
    # Suorita haku
    $search     = API::workoutSearch($_POST);
    $startdate  = arrGet($_POST,"startdate");
    $enddate    = arrGet($_POST,"enddate");
    
    # Haettava data
    $workouts       = 0;
    $duration       = 0;
    $exercises      = 0;
    $sets           = 0;
    $repetitions    = 0;
    $workoutsPerDay = 0;
    $durPerWorkout  = 0;
    $exrcPerWorkout = 0;
    $setsPerExr     = 0;
    $repsPerSet     = 0;
    
    $list = arrGet($search,"list",array());
    if (!empty($list)) {
        $workouts = count($list);
        
        foreach ($list as $workout) {
            $duration       += $workout["duration_min"];
            $exercises      += $workout["exercises"];
            $sets           += $workout["sets"];
            $repetitions    += $workout["repetitions"];
        }
        
        # Harjoituksia per päivä
        if ($startdate && $enddate) {
            $days = (strtotime($enddate)-strtotime($startdate))/60/60/24;
            if ($days > 0) {
                $workoutsPerDay = $workouts/$days;
            }
        }
        
        # Kesto per harjoitus ja harjoitteet per harjoitus
        if ($workouts > 0) {
            $durPerWorkout = $duration/$workouts;
            $exrcPerWorkout = $exercises/$workouts;
        }
        
        # Sarjoja per harjoite
        if ($exercises > 0) {
            $setsPerExr = $sets/$exercises;
        }
        
        # Toistoja per sarja
        if ($sets > 0) {
            $repsPerSet = $repetitions/$sets;
        }
    }
?>

<table class="table table-hover">
    <thead>
        <tr>
            <th class="col-md-2">
                &nbsp;
            </th>
            <th class="col-md-2">
                Total
            </th>
            <th class="col-md-8">
                Per...
            </th>
        </tr>
    </thead>
    
    <tbody>
        <tr>
            <td>
                <strong>
                    Workouts
                </strong>
            </td>
            <td>
                <?php echo $workouts; ?>
            </td>
            <td>
                <?php echo round($workoutsPerDay,2); ?>
                (per day)
            </td>
        </tr>
        <tr>
            <td>
                <strong>
                    Duration (min)
                </strong>
            </td>
            <td>
                <?php echo $duration; ?>
            </td>
            <td>
                <?php echo round($durPerWorkout,2); ?>
                (per workout)
            </td>
        </tr>
        <tr>
            <td>
                <strong>
                    Exercises
                </strong>
            </td>
            <td>
                <?php echo $exercises; ?>
            </td>
            <td>
                <?php echo round($exrcPerWorkout,2); ?>
                (per workout)
            </td>
        </tr>
        <tr>
            <td>
                <strong>
                    Sets
                </strong>
            </td>
            <td>
                <?php echo $sets; ?>
            </td>
            <td>
                <?php echo round($setsPerExr,2); ?>
                (per exercise)
            </td>
        </tr>
        <tr>
            <td>
                <strong>
                    Repetitions
                </strong>
            </td>
            <td>
                <?php echo $repetitions; ?>
            </td>
            <td>
                <?php echo round($repsPerSet,2); ?>
                (per set)
            </td>
        </tr>
    </tbody>
</table>